package com.shuoyue.ycgk.ui.estimate;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Sourt;
import com.shuoyue.ycgk.entity.SourtInfo;
import com.shuoyue.ycgk.entity.Unit;
import com.shuoyue.ycgk.ui.mok.SortAdapter;
import com.shuoyue.ycgk.utils.GlideUtil;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import butterknife.BindView;

public class FragmentSoutByJob extends BaseMvpFragment<SourtContract.Presenter>
        implements SourtContract.View {
    int evaId;
    @BindView(R.id.postName)
    TextView postName;
    @BindView(R.id.unitName)
    TextView unitName;
    @BindView(R.id.personNum)
    TextView personNum;
    @BindView(R.id.education)
    TextView education;
    @BindView(R.id.skill)
    TextView skill;
    @BindView(R.id.my_head)
    ImageView myHead;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.myScore)
    TextView myScore;
    @BindView(R.id.sort)
    TextView sort;
    @BindView(R.id.jonNum)
    TextView jonNum;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.signNum)
    TextView signNum;

    ListWithPage listWithPage;
    SortAdapter sortAdapter;


    public static FragmentSoutByJob getInstance(int evaId) {
        FragmentSoutByJob f = new FragmentSoutByJob();
        Bundle bundle = new Bundle();
        bundle.putInt("evaId", evaId);
        f.setArguments(bundle);
        return f;
    }

    @Override
    public void initParams(Bundle bundle) {
        evaId = bundle.getInt("evaId");

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_es_sort_job;
    }

    @Override
    protected void initView() {
        recycleView.setLayoutManager(new LinearLayoutManager(mContext));
        recycleView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
        sortAdapter = new SortAdapter(null);
        recycleView.setAdapter(sortAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getSourt(evaId, 1, 1);
            refreshLayout.finishRefresh();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getSourt(evaId, listWithPage.getCurrent() + 1, 1);
            refreshLayout.finishLoadMore();
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new SourtContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getUnitInfo(evaId);
        new Handler().postDelayed(() -> mPresenter.getSourt(evaId, 1, 1), 800);
    }

    @Override
    public void getSourtSuc(SourtInfo list) {
        Sourt myS = list.getMyScore();
        if (myS != null) {
            name.setText(myS.getUsername());
            myScore.setText(myS.getScore() + "");
            sort.setText(myS.getRank() + "");
            GlideUtil.loadImageCircular(mContext, myS.getAvatar(), R.mipmap.index_head, myHead);
        }
        jonNum.setText(list.getTotalPerson() > 217 ? "仅展示前217名" : "共" + list.getTotalPerson() + "人参加");

        listWithPage = list.getPage();
        if (list.getPage().getCurrent() == 1) {
            sortAdapter.resetData(list.getPage().getRecords());
        } else {
            sortAdapter.addData(list.getPage().getRecords());
        }
        registEmptyViewSimpleText(sortAdapter, "还没有排行");

//        refreshlayout.setEnableLoadMore(list.getPage().getCurrent() != list.getPage().getPages());
//        refreshlayout.finishRefresh();
//        refreshlayout.finishLoadMore();
    }

    @Override
    public void getUnitSuc(Unit unit) {
        if (unit == null) {
            return;
        }
        unitName.setText(unit.getPostUnit());
        postName.setText(unit.getPostName());
        personNum.setText(unit.getPersonNum() + "");
        signNum.setText(TextUtils.isEmpty(unit.getTotalSign()) ? "统计中" : unit.getTotalSign());
        education.setText(unit.getEducation());
        skill.setText(unit.getMajor());

    }

    @Override
    public String getTitle() {
        return "岗位排行";
    }
}
